package cn.com.yusys.yusp.admin.service;

import cn.com.yusys.yusp.admin.domain.AdminSmCrelStra;
import cn.com.yusys.yusp.admin.repository.mapper.AdminSmCrelStraMapper;
import cn.com.yusys.yusp.commons.mapper.CommonMapper;
import cn.com.yusys.yusp.commons.service.CommonService;
import cn.com.yusys.yusp.commons.util.DateUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @项目名称: yusp-admin
 * @类名称: AdminSmCrelStraService
 * @类描述: 认证策略管理服务层
 * @功能描述:
 * @创建人: dusong@yusys.com.cn
 * @创建时间: 2017-12-19 13:43
 * @修改备注:
 * @修改记录: 修改时间 修改人员 修改原因
 *        -------------------------------------------------------------
 * @version 1.0.0
 * @Copyright (c) 2017宇信科技-版权所有
 */
@Service
public class AdminSmCrelStraService extends CommonService {

    private final Logger logger = LoggerFactory.getLogger(AdminSmCrelStraService.class);
    
    @Autowired
    private AdminSmCrelStraMapper adminSmAuthRecoMapper;

    @SuppressWarnings("rawtypes")
    @Override
    protected CommonMapper getMapper() {
        return this.adminSmAuthRecoMapper;
    }

    /**
     * @throws SQLException
     * @方法名称: batchUpdate
     * @方法描述: 批量修改认证策略
     * @参数与返回说明:
     * @算法描述: 删除原认证策略后，在新增认证策略
     */
    @Transactional(readOnly = false, rollbackFor = { Exception.class, RuntimeException.class })
    public int batchUpdate(List<AdminSmCrelStra> crelList)  {
        int result = 0;
        // 删除原有配置数据
        AdminSmCrelStra adminSmCrelStraOld = new AdminSmCrelStra();
        adminSmCrelStraOld.setSysId(crelList.get(0).getSysId());
        try {
            adminSmAuthRecoMapper.delete(adminSmCrelStraOld);
            for (AdminSmCrelStra adminSmCrelStraNew : crelList) {
                adminSmCrelStraNew.setLastChgDt(DateUtil.formatDateTimeByDef());
                logger.debug("adminSmCrelStraNew:{}", adminSmCrelStraNew);
                adminSmAuthRecoMapper.insert(adminSmCrelStraNew);
                result++;
            }
            
            logger.info("Batch modification of authentication policy succeeded，result:{}", result);
        } catch (Exception e) {
            logger.error("batchUpdate error, message:{}", e.getMessage());
            throw e;
        } 
        return result;
    }
    

}
